import collections


class Solution:
    def maxSlidingWindow(self, nums: list, k: int) -> list:
        deque = collections.deque()
        ans = []
        for i in range(len(nums)):
            if deque and i - deque[0] == k:
                deque.popleft()
            while deque and nums[i] > nums[deque[-1]]:
                deque.pop()
            deque.append(i)
            if i >= k - 1:
                ans.append(nums[deque[0]])

        return ans
